home *** CD-ROM | disk | FTP | other *** search
- /* Copyright (c) Silicon Graphics, Inc. 1996 */
-
- /* blendCollage.c
- *
- * Demonstrates how to use the GL_EXT_blend_color extensions
- * to blend images that do not have an alpha channel.
- *
- * This program displays images at random positions in a window,
- * and uses a constant blending function to blend the images.
- * The blending factor can be changed interactively.
- * An option allows the images to be zoomed by a randow zoom
- * factor. Up to MAX_IMAGES can be read. To run, type:
- *
- * blendCollage <imageFile1.rgb> [<imageFile2.rgb>] ...
- *
- * Try "blendCollage /usr/demos/data/textures/*.rgb"
- *
- * LEFT Mousebutton, down - randomly choose a new image
- * Up Arrow - increase blend factor
- * Down Arrow - decrease blend factor
- * <c> key - clear window to background color
- * SPACEBAR - toggle fixed/random zoom
- * Escape key - exit the program
- *
- * David Marsland, MTS, SGI Education R & D, 1993
- */
- #include <GL/gl.h>
- #include <GL/glu.h>
- #include <GL/glut.h>
-
- #include <math.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include "rgbImageFile.h"
-
- /* Function Prototypes */
-
- GLvoid initgfx( GLvoid );
- GLvoid animate( GLvoid );
- GLvoid visibility( int );
- GLvoid drawScene( GLvoid );
- GLvoid reshape( GLsizei, GLsizei );
- GLvoid keyboard( GLubyte, GLint, GLint );
- GLvoid specialkeys( GLint, GLint, GLint );
- GLvoid mouse( GLint, GLint, GLint, GLint );
-
- void printHelp( char * );
-
- /* Global Definitions */
-
- #define KEY_ESC 27 /* ascii value for the escape key */
-
- /* Global Variables */
-
- #define MAX_IMAGES 100
-
- typedef struct _image {
- GLuint *image;
- int width, height;
- } ImageInfo;
-
- static ImageInfo images[MAX_IMAGES];
- static ImageInfo *curImage = NULL;
- static GLint imageCount;
- static GLint xPos, yPos;
-
- static GLsizei winWidth, winHeight;
- static GLsizei screenWidth, screenHeight;
-
- static GLboolean zoomFlag = GL_FALSE;
-
- static GLfloat blend_factor = 0.5;
-
- GLvoid
- main ( int argc, char *argv[] )
- {
- int i;
-
- glutInit( &argc, argv );
-
- if (argc < 2) {
- fprintf(stderr,
- "usage: %s <imageFile1> [<imageFile2> ... ]\n", argv[0] );
- exit (1);
- }
-
- imageCount = argc - 1;
- if (imageCount > MAX_IMAGES) imageCount = MAX_IMAGES;
-
- srand48( imageCount ); /* seed random number generator */
-
- /* read in all image files specifed as command line args */
- for ( i = 0; i < imageCount; i++ ) {
- images[i].image = rgbReadImageFile( argv[i+1],
- &images[i].width, &images[i].height );
- }
- curImage = &images[0];
-
- /* create a window that is 3/4 the size of the screen */
-
- screenWidth = glutGet( GLUT_SCREEN_WIDTH );
- screenHeight = glutGet( GLUT_SCREEN_HEIGHT );
- winWidth = 3*screenWidth/4;
- winHeight = 3*screenHeight/4;
- glutInitWindowPosition( winWidth / 8, winHeight / 8 );
- glutInitWindowSize( winWidth, winHeight );
- glutInitDisplayMode( GLUT_RGBA | GLUT_SINGLE );
- glutCreateWindow( argv[0] );
-
- initgfx();
-
- glutMouseFunc( mouse );
- glutKeyboardFunc( keyboard );
- glutSpecialFunc( specialkeys );
- glutReshapeFunc( reshape );
- glutIdleFunc( animate );
- glutVisibilityFunc( visibility );
- glutDisplayFunc( drawScene );
-
- printHelp( argv[0] );
-
- glutMainLoop();
- }
-
- GLvoid
- printHelp( char *progname )
- {
- fprintf(stdout, "\n\n%s - creates a collage of images "
- "which can be zoomed\n\n"
- "UP Arrow - increase blend factor\n"
- "DOWN Arrow - decrease blend factor\n"
- "<c> Key - clear the display\n"
- "SPACEBAR - toggle fixed/random zoom\n"
- "Escape key - exit the program\n\n",
- progname);
- fprintf(stdout, "blend_factor = %4.2f\n", blend_factor );
- }
-
- void
- initgfx( void )
- {
- glClearColor( 0.0, 0.0, 0.0, 1.0 );
- glClear( GL_COLOR_BUFFER_BIT );
-
- glEnable( GL_BLEND );
- glBlendColorEXT( 0, 0, 0, blend_factor );
- glBlendFunc( GL_CONSTANT_ALPHA_EXT, GL_ONE_MINUS_CONSTANT_ALPHA_EXT );
- }
-
- GLvoid
- keyboard( GLubyte key, GLint x, GLint y )
- {
- switch (key) {
- case ' ':
- zoomFlag = !zoomFlag;
- if ( !zoomFlag )
- glPixelZoom( 1.0, 1.0 );
- glutPostRedisplay();
- break;
- case 'c':
- glClear( GL_COLOR_BUFFER_BIT );
- glutPostRedisplay();
- break;
-
- case KEY_ESC: /* Exit whenever the Escape key is pressed */
- exit(0);
- }
- }
-
- GLvoid
- specialkeys( GLint key, GLint x, GLint y )
- {
- switch (key) {
- case GLUT_KEY_UP: /* increase blend factor */
- if (blend_factor < 1.0) blend_factor += 0.1;
- break;
- case GLUT_KEY_DOWN: /* decrease blend factor */
- if (blend_factor > 0.0) blend_factor -= 0.1;
- break;
- }
- printf( "blend_factor = %4.2f\n", blend_factor );
- glBlendColorEXT( 0, 0, 0, blend_factor );
- glutPostRedisplay();
- }
-
- GLvoid
- mouse( GLint button, GLint state, GLint x, GLint y )
- {
- if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
- /* grab the new mouse location */
- xPos = x;
- yPos = winHeight - y;
-
- /* pick a new image index */
-
- curImage = &images[(int) (imageCount * drand48())];
- }
- }
-
- GLvoid
- reshape( GLsizei width, GLsizei height )
- {
- glViewport( 0, 0, width - 1, height - 1);
-
- winWidth = width;
- winHeight = height;
-
- glMatrixMode( GL_PROJECTION );
- glLoadIdentity();
- gluOrtho2D( 0.0, (GLdouble) winWidth, 0.0, (GLdouble) winHeight );
- glMatrixMode( GL_MODELVIEW );
- glLoadIdentity();
- glTranslatef( 0.375, 0.375, 0.0 );
- glClear( GL_COLOR_BUFFER_BIT );
- }
-
- GLvoid
- animate( GLvoid )
- {
- /* if zooming is enabled, randomly change the zoom factor */
- if ( zoomFlag ) {
- /* generates zoom values in the range [ -2.0, 2.0 ) */
- glPixelZoom( ( drand48() * 4.0 ) - 2.0,
- ( drand48() * 4.0 ) - 2.0 );
- }
-
- /* Tell GLUT to redraw the scene */
- glutPostRedisplay();
- }
-
- GLvoid
- visibility( int state )
- {
- if (state == GLUT_VISIBLE) {
- glutIdleFunc( animate );
- } else {
- glutIdleFunc( NULL );
- }
- }
-
- GLvoid
- drawScene( GLvoid )
- {
- xPos += (2.0*drand48() - 1.0)*.05*screenWidth;
- yPos += (2.0*drand48() - 1.0)*.05*screenHeight;
- xPos = fabs( xPos );
- yPos = fabs( yPos );
- glRasterPos2i( xPos, yPos );
- glDrawPixels( curImage->width, curImage->height,
- GL_RGBA, GL_UNSIGNED_BYTE, curImage->image );
- glFlush();
- }
-